﻿#ifndef BAIDUOCRONLINE_H
#define BAIDUOCRONLINE_H

#include <QObject>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QUrl>
#include <QUrlQuery>
#include <QNetworkReply>
#include <QMessageBox>
#include <QImage>
#include <QFile>
#include <QTextStream>
#include <QBuffer>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <QTimer>

#define id_card_fside 0
#define id_card_bside 1

class BaiduOCROnline : public QObject
{
    Q_OBJECT
public:
    explicit BaiduOCROnline(QObject *parent = nullptr);

    void https_ssl_config(QNetworkRequest& NetworkRequest);
    QString image2base64_str(const QImage& img);
    QString image2base64_str(const QString& imgpath);
    QImage base64_str2image(const QString & base64_str);
    void writeAccToken(const QString& token);
    QString readAccToken();

public slots:
    void getAccToken(const QString& API_Key, const QString& Secret_Key); //获取鉴权码
    void StdOCR(const QString& imagepath);   //标准OCR
    void PreciseOCR(const QString& imagepath);   //高精度OCR
    void idCardScan(const QString& imagepath, quint8 id_card_side);  //身份证
    void bankCardScan(const QString& imagepath);    //银行卡
    void vehicleCardScan(const QString& imagepath);   //行驶证
    void driveCardScan(const QString& imagepath);   //驾驶证
    void licensePlateScan(const QString& imagepath); //车牌
    void vat_invoiceScan(const QString& imagepath);  //增值税发票
    void quota_invoiceScan(const QString& imagepath);  //增值税发票
    void family_bookScan(const QString& imagepath);  //户口本识别

private slots:
    void getAccTokenReply(QNetworkReply* reply);
    void StdOCRReply(QNetworkReply* reply);
    void PreciseOCRReply(QNetworkReply* reply);
    void idCardScanReply(QNetworkReply* reply);
    void bankCardScanReply(QNetworkReply* reply);
    void vehicleCardScanReply(QNetworkReply* reply);
    void driveCardScanReply(QNetworkReply* reply);
    void licensePlateScanReply(QNetworkReply* reply);
    void vat_invoiceScanReply(QNetworkReply* reply);
    void quota_invoiceScanReply(QNetworkReply* reply);
    void family_bookScanReply(QNetworkReply* reply);

signals:
    void StdOCRResult(const QString& content);
    void PreciseOCRResult(const QString& content);
    void idCardScanResult(const QString& content);
    void bankCardScanResult(const QString& content);
    void vehicleCardScanResult(const QString& content);
    void driveCardScanResult(const QString& content);
    void licensePlateScanResult(const QString& content);
    void vat_invoiceScanResult(const QString& content);
    void quota_invoiceScanResult(const QString& content);

    void family_bookScanResult(const QMap<QString, QString>& content);
    void family_bookScanError(const QString& err);
    void family_bookGoOn();

private:
    QNetworkAccessManager* NetAccManager;
    QNetworkRequest NetRequest;
    /* 应用参数 */
    QString AccToken = "";
    /* 应用参数end */
    const QString StdOCRUrl = "https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic";
    const QString PreciseOCRUrl = "https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic";
    const QString idCardUrl = "https://aip.baidubce.com/rest/2.0/ocr/v1/idcard";
    const QString bankCardUrl = "https://aip.baidubce.com/rest/2.0/ocr/v1/bankcard";
    const QString vehicleCardUrl = "https://aip.baidubce.com/rest/2.0/ocr/v1/vehicle_license";
    const QString driveCardUrl = "https://aip.baidubce.com/rest/2.0/ocr/v1/driving_license";
    const QString licensePlateUrl = "https://aip.baidubce.com/rest/2.0/ocr/v1/license_plate";
    const QString vat_invoicedUrl = "https://aip.baidubce.com/rest/2.0/ocr/v1/vat_invoice";
    const QString quota_invoicedUrl = "https://aip.baidubce.com/rest/2.0/ocr/v1/quota_invoice";
    const QString family_bookUrl = "https://aip.baidubce.com/rest/2.0/ocr/v1/household_register";
};

#endif // BAIDUFACEONLINE_H
